package com.example.blog.dao;

import com.example.blog.entity.Comments;
import com.example.blog.entity.PostsInfo;
import com.example.blog.entity.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: FoodInfoRespository
 * @Description:

 * @Date 2023/9/9
 * @Version 1.0
 */
@Repository
public interface UserInfoRepository extends JpaRepository<UserInfo,String> , JpaSpecificationExecutor<UserInfo> {
    /**
     * 查询用户
     * @param userName
     * @param password
     * @return
     */
    UserInfo findByusernameAndPassword(String userName,String password);

    UserInfo findByusername(String username);

//    @Query(value = "SELECT t.*,r.* from userInfo t left join user_roles r on t.id = r.userId where r.userId = ?1",nativeQuery = true)
//    List<Map<String, String>> findByUserId(String id);
    @Query(value = "SELECT u.id userId, s.* FROM userInfo u LEFT JOIN user_role ur ON u.id = ur.userId LEFT JOIN (SELECT r.id AS roleId,r.rName,ps.id AS permissionId,ps.pName FROM roles r LEFT JOIN role_permissions p ON r.id = p.role_id LEFT JOIN permissions ps ON p.permission_id = ps.id ) s ON ur.roleId = s.roleId where u.id = ?1",nativeQuery = true)
    List<Map<String, String>> findByUserId(String id);
}
